package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced;


import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.AbstractHandleHttpReportClientProxyClientTransferCloseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;


/**
 * 目标客户端传输通道连接初始化成功
 * HTTP_REPORT_CLIENT_PROXY_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL_
 */
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleHttpReportClientProxyClientTransferCloseTypeAdvanced
        extends AbstractHandleHttpReportClientProxyClientTransferCloseTypeAdvanced<NettyProxyMsg> {


    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {

        Channel channel = nettyChannelContext.channel();
        // 关闭通道

        // 关闭 next、next transfer 通道

        Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
        Channel transferNextChannel = ChannelAttributeKeyUtils.getTransferNextChannel(channel);

        // 下发关闭客户端真实通道
        NettyProxyMsg closeTransferNettyProxyMsg = new NettyProxyMsg();
        closeTransferNettyProxyMsg.setType(ProxyMessageType.HTTP_DISTRIBUTE_CLIENT_PROXY_CLIENT_TRANSFER_CLOSE_);
        transferNextChannel.writeAndFlush(closeTransferNettyProxyMsg);

        channel.close();
        nextChannel.close();
        transferNextChannel.close();
    }

}
